﻿Orphans = LibStub("AceAddon-3.0"):NewAddon("Orphans", "AceConsole-3.0", "AceEvent-3.0")

local options = {
	name = "Orphans",
	handler = Orphans,
	type = 'group',
	args = {
		blockParty = {
			type = "toggle",
			name = "Block Party Invitations",
			desc = "Block Party Invitations?",
			get = "IsBlockPartyInvitations",
			set = "ToggleBlockPartyInvitations",
		},
		blockGuild = {
			type = "toggle",
			name = "Block Guild Invitations",
			desc = "Block Guild Invitations?",
			get = "IsBlockGuildInvitations",
			set = "ToggleBlockGuildInvitations",
		},
		testCharacter = {
			type = "input",
			name = "Character",
			desc = "Test Character Name",
			usage = "<Name of Character to Test>",
			get = "GetTestCharacter",
			set = "SetTestCharacter",
		},
		test = {
			type = "execute",
			name = "Test",
			desc = "Test Block Functionality",
			func = "Test",
		},
	},
}

local defaults = {
	profile = {
		blockPartyInvitations = false,
		blockGuildInvitations = false,
		testCharacter = "Sumobear",
	}
}

function Orphans:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("OrphansDB", defaults, "Default")

	LibStub("AceConfig-3.0"):RegisterOptionsTable("Orphans", options)
    
    self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Orphans", "Orphans")
    
    self:RegisterChatCommand("orphans", "ChatCommand")
    self:RegisterChatCommand("orph", "ChatCommand")
end

function Orphans:OnEnable()
	self:Print("Orphans Loaded and is protecting you")
	
    self:RegisterEvent("PARTY_INVITE_REQUEST")
end

function Orphans:OnDisable()
    -- Called when the addon is disabled
end


function Orphans:ChatCommand(input)
	if not input or input:trim() == "" then
		InterfaceOptionsFrame_OpenToCategory(self.optionsFrame)
	else
		LibStub("AceConfigCmd-3.0").HandleCommand(Orphans, "orph", "Orphans", input)
	end
end


function Orphans:PARTY_INVITE_REQUEST(eventName, leaderName)
    self:Print("Party Invitation: " .. leaderName)
    self:Print("Block Party Invitations: " .. self.db.profile.blockPartyInvitations)

	if self.db.profile.blockPartyInvitations then
		local isFriend = self:ExistsInFriendsList(leaderName)
		local isGuildie = self:ExistsInGuildList(leaderName)
		
		self:Print("  " .. leaderName .. ": " .. isFriend .. ", " .. tostring(isGuildie))
		
		if not isFriend then
			DeclineGroup()
		end
	end
end


function Orphans:IsBlockPartyInvitations(info)
	return self.db.profile.blockPartyInvitations
end

function Orphans:ToggleBlockPartyInvitations(info, value)
	self.db.profile.blockPartyInvitations = value
end


function Orphans:IsBlockGuildInvitations(info)
	return self.db.profile.blockGuildInvitations
end

function Orphans:ToggleBlockGuildInvitations(info, value)
	self.db.profile.blockGuildInvitations = value
end


function Orphans:GetTestCharacter(info)
	return self.db.profile.testCharacter
end

function Orphans:SetTestCharacter(info, value)
	self.db.profile.testCharacter = value
end


function Orphans:Test()
	if self.db.profile.blockPartyInvitations then
		local isFriend = self:ExistsInFriendsList(self.db.profile.testCharacter)
		local isGuildie = self:ExistsInGuildList(self.db.profile.testCharacter)
		
		self:Print("  " .. self.db.profile.testCharacter .. ": " .. tostring(isFriend) .. ", " .. tostring(isGuildie))
	end
end


function Orphans:ExistsInFriendsList(character)
	local foundMatch = false
	
	local numFriends = GetNumFriends()
	
	self:Print("Friends: " .. numFriends)
	
	for i = 1, numFriends, 1 do
		local isMatch = false
		local name, level, class, loc, connected, status = GetFriendInfo(i)
		
		if name == character then
			isMatch = true
			foundMatch = true
		end
		
		self:Print("  " .. i .. ": " .. name .. " (" .. tostring(isMatch) .. ")")
	end
	
	return foundMatch
end


function Orphans:ExistsInGuildList(character)
	local foundMatch = false
	
	local numGuildies = GetNumGuildMembers(false)
	
	self:Print("Guildies: " .. numGuildies)
	
	for i = 1, numGuildies, 1 do
		local isMatch = false
		local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName = GetGuildRosterInfo(i);
		
		if name == character then
			isMatch = true
			foundMatch = true
		end
		
		self:Print("  " .. i .. ": " .. name .. " (" .. tostring(isMatch) .. ")")
	end
	
	return foundMatch
end
